package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.SaleListDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.entity.vo.CustomerGoodsVo;
import com.atguigu.jxc.entity.vo.SaleDataByDayOrMonthVo;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qingyu
 * @create 2022-01-02 18:37
 * @description
 */

@Service
public class SaleListGoodsServiceImpl implements SaleListGoodsService {

    @Autowired
    private SaleListGoodsDao saleListGoodsDao;
    @Autowired
    private SaleListDao saleListDao;
    @Autowired
    private GoodsDao goodsDao;


    /**
     * 新增销售单，并减库存
     * @param saleList
     * @param saleListGoodsStr
     * @param saleNumber
     */
    @Override
    public void save(SaleList saleList, String saleListGoodsStr, String saleNumber) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<SaleListGoods>>() {}.getType();
        List<SaleListGoods> saleListGoodsList = gson.fromJson(saleListGoodsStr, type);
        for (SaleListGoods saleListGoods : saleListGoodsList) {
            String goodsCode = saleListGoods.getGoodsCode();
            Integer integer = goodsDao.getGood(goodsCode);
            Integer goodsNum = saleListGoods.getGoodsNum();
            goodsNum = integer - goodsNum;
            goodsDao.update(goodsCode, goodsNum);
            saleListGoodsDao.insert(saleListGoods);
        }
        saleList.setUserId(1);
        saleListDao.save(saleList);
    }

    /**
     * 查询销售单商品信息
     * @param saleListId
     * @return
     */
    @Override
    public List<SaleListGoods> goodsList(Integer saleListId) {
        List<SaleListGoods> goodsList = saleListGoodsDao.goodList(saleListId);
        return goodsList;
    }

    /**
     * 通过saleListId删除销售单商品信息
     * @param saleListId
     */
    @Override
    public void delete(Integer saleListId) {
        saleListGoodsDao.delete(saleListId);
    }

    @Override
    public void updateState(Integer saleListId) {
        saleListGoodsDao.updateState(saleListId);
    }

    @Override
    public List<CustomerGoodsVo> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {

        List<CustomerGoodsVo> goodsVos = saleListGoodsDao.count(sTime,eTime,goodsTypeId,codeOrName);

        return goodsVos;
    }

    @Override
    public List<SaleDataByDayOrMonthVo> getSaleDataByDay(String sTime, String eTime) {

        List<SaleDataByDayOrMonthVo> saleDataByDayOrMonthVos = new ArrayList<>();
        try {
            List<String> timeSlotByDay = DateUtil.getTimeSlotByDay(sTime, eTime);
            if (!CollectionUtils.isEmpty(timeSlotByDay)){
                for (String s : timeSlotByDay) {
                    SaleDataByDayOrMonthVo byDayVo = new SaleDataByDayOrMonthVo();
                    Double saleTotal =0.0;
                    Double purchasingTotal =0.0;
                    Double profit =0.0;

                    //根据日期查询当日销售额情况
                    if (saleListGoodsDao.getSaleTotal(s) != null){
                        saleTotal = saleListGoodsDao.getSaleTotal(s);
                    }

                    //根据日期查询当日成本情况
                    if (saleListGoodsDao.getPurchasingTotal(s)!=null){
                        purchasingTotal = saleListGoodsDao.getPurchasingTotal(s);
                    }

                    //当日利润情况
                    profit = saleTotal - purchasingTotal;

                    byDayVo.setDate(s);
                    byDayVo.setSaleTotal(saleTotal);
                    byDayVo.setPurchasingTotal(purchasingTotal);
                    byDayVo.setProfit(profit);

                    saleDataByDayOrMonthVos.add(byDayVo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return saleDataByDayOrMonthVos;
    }

    /*@Override
    public List<SaleDataByDayOrMonthVo> getSaleDataByDay(String sTime, String eTime) {

        List<SaleDataByDayOrMonthVo> byDayVos = saleListGoodsDao.getSaleDataByDay(sTime,eTime);
        if (!CollectionUtils.isEmpty(byDayVos)){
            for (SaleDataByDayOrMonthVo byDayVo : byDayVos) {
                byDayVo.setProfit(byDayVo.getSaleTotal() - byDayVo.getPurchasingTotal());
            }
        }

        return byDayVos;
    }*/

    @Override
    public List<SaleDataByDayOrMonthVo> getSaleDataByMonth(String sTime, String eTime) {

        List<SaleDataByDayOrMonthVo> saleDataByDayOrMonthVos = new ArrayList<>();
        try {
            List<String> timeSlotByMonth = DateUtil.getTimeSlotByMonth(sTime, eTime);
            if (!CollectionUtils.isEmpty(timeSlotByMonth)){
                for (String s : timeSlotByMonth) {
                    SaleDataByDayOrMonthVo byDayVo = new SaleDataByDayOrMonthVo();
                    Double saleTotal =0.0;
                    Double purchasingTotal =0.0;
                    Double profit =0.0;

                   //根据月份查询当月销售额情况
                    if (saleListGoodsDao.getSaleTotal(s) != null){
                        saleTotal = saleListGoodsDao.getSaleTotal(s);
                    }

                    //根据月份查询当月成本情况
                    if (saleListGoodsDao.getPurchasingTotal(s)!=null){
                        purchasingTotal = saleListGoodsDao.getPurchasingTotal(s);
                    }

                    //当月利润情况
                    profit = saleTotal - purchasingTotal;

                    byDayVo.setDate(s);
                    byDayVo.setSaleTotal(saleTotal);
                    byDayVo.setPurchasingTotal(purchasingTotal);
                    byDayVo.setProfit(profit);

                    saleDataByDayOrMonthVos.add(byDayVo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return saleDataByDayOrMonthVos;
    }
}
